package common.core
{
	import flash.display.Bitmap;
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Rectangle;
	import flash.utils.Dictionary;

	/**
	 * 清除内部所有子对象的基本Sprite 
	 * @author shishujun
	 * 
	 */	
	public class GSprite extends Sprite
	{
		private var m_Data : *;			//使用过程传递数据
		private var m_Width : Number;			//宽
		private var m_Height : Number;			//高
		
		private var m_bodyRange : Rectangle;    //身体范围
		private var m_showBodyRange : Boolean;
		
		private var m_autoRange : Boolean;
		
		private var m_eventListenerLst : Object;
		
		public function GSprite()
		{
			super();
			m_Width = 0;
			m_Height = 0;
			m_autoRange = true;
			m_eventListenerLst = new Object;
//			this.addEventListener(Event.REMOVED, onRemove);
		}
		
		public override function addChild(child:DisplayObject):DisplayObject
		{
			super.addChild(child);
			
			if(!m_autoRange) return child;;
			
			if(m_Width < child.width + child.x)m_Width = child.width + child.x;
			if(m_Height < child.height + child.y)m_Height = child.height + child.y;
			
			drawBckgound();

			return child;
		}
		
		public override function removeChild(child:DisplayObject):DisplayObject
		{
			super.removeChild(child);
			
			if(!m_autoRange) return child;
			
			m_Width = 0;
			m_Height = 0;
			
			for(var i:int=0;i<this.numChildren;i++)
			{
				var item : DisplayObject = this.getChildAt(i);
				
				if(item.width > m_Width) m_Width = item.width + item.x;
				if(item.height > m_Height) m_Height = item.height + item.y;
			}
			
			drawBckgound();
			
			return child;
		}
		
		/**
		 * 写入数据 
		 * @param data		数据
		 * 
		 */		
		public function set setData(data : *) : void
		{
			m_Data = data;
		}
		
		/**
		 * 获得数据 
		 * @return 
		 * 
		 */		
		public function get Data() : *
		{
			return m_Data;
		}
		
		public override function set width(value:Number):void
		{
			m_Width = value;
			
			drawBckgound();
		}
		
		public override function get width():Number
		{	
			return m_Width;
		}
		
		public override function set height(value:Number):void
		{
			m_Height = value;
			
			drawBckgound();
		}
		
		public override function get height():Number
		{
			return m_Height;
		}
		
		/**
		 * 当对象被删除后调用删除事件 
		 * @param event
		 * 
		 */		
		private function onRemove(event : Event) : void
		{
//			this.removeEventListener(Event.REMOVED, onRemove);
			if(event.target == event.currentTarget)cleanChildren(this);
		}
		
		/**
		 * 递归删除子对象 
		 * @param _this
		 * 
		 */		
		public function cleanChildren(_this : Object) : void
		{
			if ( DisplayObjectContainer(_this) is MovieClip )MovieClip(DisplayObjectContainer(_this)).stop();
			while (DisplayObjectContainer(_this).numChildren > 0 )
			{
				var child:DisplayObject = DisplayObjectContainer(_this).getChildAt(0);
				
				if ( child is MovieClip )MovieClip(child).stop();
				if ( child is DisplayObjectContainer )cleanChildren(child);
				if ( child is Bitmap )Bitmap(child).bitmapData.dispose();
				
				child.filters = null;
				child.opaqueBackground = null;
				child.scale9Grid = null;                               
				child.visible = false;
//				trace("删除对象",child.name);
				DisplayObjectContainer(_this).removeChildAt(0);
			}
		}
		
		/**
		 * 获取身体范围
		 * @return  
		 */		
		public function get bodyRange() : Rectangle
		{
			if(!m_bodyRange)
			{
				m_bodyRange = new Rectangle(0,0,0,0);
			}
			
			return m_bodyRange;
		}
		
		/**
		 * 设置身体范围
		 * @return  
		 */
		public function set bodyRange(range : Rectangle) : void
		{
			m_bodyRange = range;
		}
		
		/**
		 *  设置是否可见身体范围矩形
		 */		
		public function set showBodyRange(view : Boolean) : void
		{
			m_showBodyRange = view;
			
			if(view)
			{
				this.graphics.lineStyle(1,0xFF0000);
				this.graphics.drawRect(bodyRange.left,bodyRange.top,bodyRange.width,bodyRange.height);
			}
			else
			{
				this.graphics.clear();
			}
		}
		
		/**
		 * 获取是否显示身体矩形范围
		 * @return  
		 */		
		public function get showBoyRange() : Boolean
		{
			return m_showBodyRange;
		}
		
		/**
		 * 碰撞回调
		 */		
		public function collision(object : GSprite) : void
		{
			
		}
		
		/**
		 * 
		 */		
		public function unCollision(object : GSprite) : void
		{
			
		}
		
		/**
		 * 实时更新 
		 */		
		public function update() : void
		{
			
		}
		
		/**
		 * 销毁 
		 */		
		public function destory() : void
		{
			m_Data = null;			
			m_Width = 0;			
			m_Height = 0;			
			m_bodyRange = null;    
			m_showBodyRange = false;
			m_autoRange = false;
			m_eventListenerLst = null;
		}
		
		public function drawBckgound() : void
		{
			this.graphics.clear();
			this.graphics.beginFill(0x333333,0);
			this.graphics.drawRect(0,0,m_Width,m_Height);
			this.graphics.endFill();
		}
		
		/**
		 * 设置是否自动改变窗口范围
		 * @param value
		 */		 
		private function set autoRange(value : Boolean) : void
		{
			m_autoRange = value;
		}
		
		/**
		 * 获取是否自动改变窗口范围
		 * @param value  默认为true
		 */
		private function get autoRange() : Boolean
		{
			return m_autoRange;
		}
		
		public override function addEventListener(type:String, listener:Function, useCapture:Boolean=false, priority:int=0, useWeakReference:Boolean=false):void
		{
			super.addEventListener(type,listener,useCapture,priority,useWeakReference);
			
			if(!m_eventListenerLst.hasOwnProperty(type))
			{
				m_eventListenerLst[type] = [];
			}
			
			m_eventListenerLst[type].push(listener);
		}
		
		/**
		 * 销毁 
		 */		
		public function dispose() : void
		{
			if(this.parent)
			{
				this.parent.removeChild(this);
			}
			
			removeLiseners();
			
			destory();
		}
		
		/**
		 * 删除侦听 
		 */		
		public function removeLiseners() : void
		{
			for(var type : String in m_eventListenerLst)
			{
				var funs : Array = m_eventListenerLst[type];
				var length : int = funs.length;
				
				for(var i:int=0;i<length;i++)
				{
					this.removeEventListener(type,funs[i]);
				}
			}
		}
	}
}